package com.mix.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author LiuMingxin
 * @date 2024/11/4&19:48
 */
@Slf4j
@Component
@SuppressWarnings("unused")
public class RedisUtil {

    @Autowired
    RedisTemplate redisTemplate;

    public static String REDIS_KEY_AUTH_TOKEN = "LM_AUTH_TOKEN";
    public static String REDIS_KEY_AUTH_USER_DETAIL = "LM_AUTH_USER_DETAIL";

    public void set(String k, Object v) {
        redisTemplate.opsForValue().set(k, v);
    }

    public void set(String k, Object v, Long time) {
        redisTemplate.opsForValue().set(k, v, time, TimeUnit.SECONDS);
    }

    public String get(String k) {
        String v = (String) redisTemplate.opsForValue().get(k);
        return v;
    }

    public boolean expire(String key, long timeout, TimeUnit unit) {
        Boolean result = redisTemplate.expire(key, timeout, unit);
        if (result == null) {
            throw new RedisOpsResultIsNullException();
        }
        return result;
    }

    public static class RedisOpsResultIsNullException extends NullPointerException {
        public RedisOpsResultIsNullException() {
            super();
        }

        public RedisOpsResultIsNullException(String message) {
            super(message);
        }
    }

}